package com.jackrain.nea.fc.vp.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.HashMap;
import java.util.List;

/**
 * @author xuxisnhuai
 */
@Mapper
public interface ViewCvipMapper {
    /**
     * 卡号+类型查询账户ACTIVE
     *
     * @param vipid
     * @param typeid
     * @return
     */
    @Select("SELECT ISACTIVE FROM VP_C_VIP_ACC WHERE VP_C_VIP_ID = #{arg0} AND VP_C_VIPTYPEGROUP_ID=#{arg1}")
    String selectvipaccactive(Long vipid, Long typeid);


    /**
     * 根据传入的数据查找店仓档案表中ECODE=传入值的ID
     *
     * @param ecode
     * @return
     */
    @Select("SELECT CP_C_STORE_ID FROM VP_C_VIP_ACC WHERE ECODE = #{arg0} AND VP_C_VIPTYPEGROUP_ID=#{arg1} AND VP_C_VIP_ID=#{arg2}")
    Long selectcpstoreid(String ecode, Long typeid,Long vipid);

    /**
     * 查询VIP类型ID
     *
     * @param ecode
     * @return
     */
    @Select("SELECT ID FROM VP_C_VIPTYPEGROUP WHERE ECODE = #{ecode}")
    Long selecttypegroupid(String ecode);

    /**
     * 卡号+类型查询档案表ID
     *
     * @param vipid
     * @param typeid
     * @return
     */
    @Select("SELECT ECODE FROM VP_C_VIP_ACC WHERE VP_C_VIP_ID = #{arg0} AND VP_C_VIPTYPEGROUP_ID=#{arg1}")
    String selectvipaccecode(Long vipid, Long typeid);

    /**
     * 查询
     *
     * @param param
     * @return List<HashMap>
     */
    @SelectProvider(type = CVipSqlProvider.class,
            method = "selectvipsql")
    List<HashMap> selectvipsql(@Param("list") Object[] param);


    class CVipSqlProvider {

        public String selectvipsql(final HashMap map) {
            return new SQL() {
                {

                    String colum = " ID, ENAME,SEX, BIRTHDATE,MOBIL,  ADDRESS,  JOB,REMARK,ISRECEIVEPROM,RECEIVEWAY,WECHAT,EMAIL ,OPENID,ID_CARD,ACADEMIC, ISACTIVE,MODIFIERID,MODIFIEDDATE ";

                    SELECT(colum);
                    FROM("VP_C_VIP");
                    Object[] list = (Object[]) map.get("list");
                    StringBuilder sb = new StringBuilder("(");
                    for (Object num : list) {
                        sb.append(num).append(",");
                    }
                    sb.deleteCharAt(sb.length() - 1).append(")");
                    WHERE("ID IN " + sb.toString());

                }
            }.toString();
        }
    }
}
